<?php
    
    class ElementTablePages extends Element {
        public function Render( $curpage, $numpages ) {
            global $server;
            global $db;
            global $table;

            if ( $numpages <= 1 ) {
                return;
            }

            $start = $curpage - 2;
            if ( $start < 0 ) {
                $start = 0;
            }

            $end = $start + 4;
            if ( $end >= $numpages ) {
                $end = $numpages - 1;

                $start = $end - 4;
                if ( $start < 0 ) {
                    $start = 0;
                }
            }
            
            ?><div class="pages">
                <a <?php
                if ( $curpage > 0 ) {
                    ?>href="<?php
                    Element( 'url', $server, $db, $table, $curpage - 1 );
                    ?>"<?php
                }
                else {
                    ?>class="disabled"<?php
                }
                ?> title="<?php
                if ( $curpage > 0 ) {
                    ?>Previous<?php
                }
                else {
                    ?>No previous page<?php
                }
                ?>"><img src="images/resultset_previous.png" alt="&lt;" /></a> <?php
                
                for ( $i = $start; $i <= $end; ++$i ) {
                    ?><a href="<?php
                    Element( 'url', $server, $db, $table, $i );
                    ?>" class="nextbacklinks"<?php
                    if ( $curpage == $i ) {
                        ?> style="font-weight: bold;"<?php
                    }
                    ?>><?php
                    echo $i;
                    ?></a><?php
                    if ( $i < $end ) {
                        ?>,<?php
                    }
                    ?> <?php
                }
                if ( $end < $numpages - 1 ) {
                    ?>... <?php
                }
                ?><a <?php
                if ( $curpage < $numpages - 1 ) {
                    ?>href="<?php
                    Element( 'url', $server, $db, $table, $curpage + 1 );
                    ?>"<?php
                }
                else {
                    ?>class="disabled"<?php
                }
                ?> title="<?php
                if ( $curpage < $numpages - 1 ) {
                    ?>Next<?php
                }
                else {
                    ?>No next page<?php
                }
                ?>"><img src="images/resultset_next.png" alt="&gt;" /></a>
            </div><?php
        }
    }

?>
